'use strict'

define(['jquery', 'bootstrap', 'select2', 'shiftform', 'validate', 'validate.en', 'datetimepicker', 'datetimepickerCN'], function($) {
  
  return {
        
    init: function(data) {
    	this.filter = data.filter;
    	this.name = data.name;
    	this.mobile = data.mobile;
    	
    	this.initPage();
    	this.exist();
    },
    
    initPage: function(){
    	var that = this;
    	
    	$('select[name="site"]').select2({
            minimumResultsForSearch : -1,
            allowClear : true,
            width: '150'
          });

          $('select[name="site"]').change(function(){ 
            
            
            var site = $(this).children('option:selected').val();
            
            window.location.href = window.__CTX__.root + generateUrl(site);
          })
          
          function generateUrl(site) {
            
            var url = "/offline-students?site=" + site;
            if(that.filter) {
              url = url + "&filter=" + that.filter;
            }
            
            if(that.name) {
              url = url + "&name=" + that.name;
            }
            
            if(that.mobile) {
              url = url + "&mobile=" + that.mobile;
            }
            
            return url;
          }
    },
    
    exist: function() {
        
        $(document).on('shown.bs.modal', '#student-mobile', function(e){

      	$(e.currentTarget).find('form[data-validate="true"]').bt_validate()  //bind validate
          
          var $form = $(e.currentTarget).find("form")                          //bind shiftform
          $form.shiftform({
            beforeSubmit : function(el) {
              $form.trigger("validate.ajax.submit");
              return $.bt_validate.result[$.bt_validate.form_id]
            }
          })
          
        })
      }
  }
})
